package com.halflife.parameter.icon;

import com.halflife.framework.generator.GenerateUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.util.FileCopyUtils;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class IconUtils {
    @Test
    public void test()throws Exception{


        String baseDirName = "D:\\svg\\Iconsa\\";
        String destDirName = "D:\\svg\\Iconss\\";
        File ff = new File(destDirName);
        if (!ff.exists()) {
            ff.mkdir();
        }
        File baseDir = new File(baseDirName);
        for (File subDir : baseDir.listFiles()) {
            File fss = new File(ff, subDir.getName());
            if (!fss.exists()) {
                fss.mkdir();
            }
            for (File iconFile : subDir.listFiles() ) {
                if (!iconFile.getName().contains("png")) {
                    continue;

                }
                BufferedImage bufImg = ImageIO.read(iconFile);
                int w = bufImg.getWidth();
                File destFile = new File(fss,iconFile.getName());
                if (w > 200) {
                    double radio = w*1.0/200;
                    Double h_ = bufImg.getHeight()*1.0/radio;
                    int h = h_.intValue();



                    BufferedImage to= new BufferedImage(200, h, BufferedImage.TYPE_INT_RGB);
                    Graphics2D g2d = to.createGraphics();
                    to = g2d.getDeviceConfiguration().createCompatibleImage(200, h, Transparency.TRANSLUCENT);
                    g2d.dispose();

                    g2d = to.createGraphics();
                    Image from = bufImg.getScaledInstance(200, h, Image.SCALE_AREA_AVERAGING);
                    g2d.drawImage(from, 0, 0, null);
                    g2d.dispose();

                    ImageIO.write(to,iconFile.getName().substring(iconFile.getName().lastIndexOf(".")+1), destFile); //写入缩减后的图片

                }else{
                    FileCopyUtils.copy(iconFile, destFile);
                }



            }
        }
    }
    @Test
    public void imageTest()throws Exception{
        File dir = new File("D:\\svg\\iconsvg");
        for (File subFile : dir.listFiles()) {
            String catagory = subFile.getName();
            for (File file : subFile.listFiles()) {
                if (file.getName().contains("svg")) {
                    FileCopyUtils.copy(file, new File("D:/svg/" + file.getName()));
                }
            }
        }
    }
    public static void main(String[] args) {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
        //要填入模本的数据文件

        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
        //这里我们的模板是放在com.havenliu.document.template包下面
        configuration.setClassForTemplateLoading(IconUtils.class, "/com/halflife/parameter/icon");
        File dir = new File("d:/svg/iconsvg");
        Map<String, Object> param = new HashMap<>();
        param.put("x",30);
        param.put("y",18);
        param.put("width",30);
        param.put("height",18);
        String svgCode = "";
        BASE64Encoder encoder = new BASE64Encoder();
        String baseDirName = "D:\\svg\\Iconss\\";
        File baseDir = new File(baseDirName);
        for (File subDir : baseDir.listFiles()) {
            for (File iconFile : subDir.listFiles() ) {
                try (FileInputStream fis = new FileInputStream(iconFile)) {
                    System.out.println(iconFile.getName());
                    byte[] datas = new byte[fis.available()];
                    fis.read(datas);
                    svgCode = encoder.encode(datas);
                    BufferedImage sourceImg  = ImageIO.read(iconFile);
                    sourceImg.getWidth();
                    System.out.println(sourceImg.getWidth());
                    System.out.println(sourceImg.getHeight());
                    int width = sourceImg.getWidth();
                    int height = sourceImg.getHeight();
                    int maxLength = width>height?width:height;
                    if (maxLength > 400) {
                        width = width/2;
                        height = height/2;
                    } else if (maxLength > 600) {
                        width = width/3;
                        height = height/3;
                    }else if (maxLength > 800) {
                        width = width/4;
                        height = height/4;
                    }
                    param.put("width",sourceImg.getWidth());
                    param.put("height",sourceImg.getHeight());

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                param.put("svgCode", svgCode);
                Template t=null;
                try {
                    //test.ftl为要装载的模板
                    t = configuration.getTemplate("svg.ftl");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //输出文档路径及名称
                Writer out = null;
                try {
                    GenerateUtil.mkDir(new File(dir.getAbsoluteFile()+"/"+subDir.getName()));
                    //Writer w = new OutputStreamWriter(System.out);
                    Writer w = new OutputStreamWriter(new FileOutputStream(dir.getAbsoluteFile()+"/"+subDir.getName()+"/"+iconFile.getName().replaceAll(".png",".svg")));
                    out = new BufferedWriter(w);
                    w.flush();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                try {
                    t.process(param, out);
                    out.close();
                } catch (TemplateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }


    }
    public static void convertToSvg(File iconFile,String targetDir) {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
        //要填入模本的数据文件

        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
        //这里我们的模板是放在com.havenliu.document.template包下面
        configuration.setClassForTemplateLoading(IconUtils.class, "/com/halflife/parameter/icon");
        Map<String, Object> param = new HashMap<>();
        param.put("x",30);
        param.put("y",18);
        param.put("width",30);
        param.put("height",18);
        String svgCode = "";
        BASE64Encoder encoder = new BASE64Encoder();
        try (FileInputStream fis = new FileInputStream(iconFile)) {
            System.out.println(iconFile.getName());
            byte[] datas = new byte[fis.available()];
            fis.read(datas);
            svgCode = encoder.encode(datas);
            BufferedImage sourceImg  = ImageIO.read(iconFile);
            sourceImg.getWidth();
            System.out.println(sourceImg.getWidth());
            System.out.println(sourceImg.getHeight());
            int width = sourceImg.getWidth();
            int height = sourceImg.getHeight();
            int maxLength = width>height?width:height;
            if (maxLength > 400) {
                width = width/2;
                height = height/2;
            } else if (maxLength > 600) {
                width = width/3;
                height = height/3;
            }else if (maxLength > 800) {
                width = width/4;
                height = height/4;
            }
            param.put("width",sourceImg.getWidth());
            param.put("height",sourceImg.getHeight());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        param.put("svgCode", svgCode);
        Template t=null;
        try {
            //test.ftl为要装载的模板
            t = configuration.getTemplate("svg.ftl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //输出文档路径及名称
        Writer out = null;
        try {
             //Writer w = new OutputStreamWriter(System.out);
            Writer w = new OutputStreamWriter(new FileOutputStream(targetDir+"/"+iconFile.getName().replaceAll(".png",".svg")));
            out = new BufferedWriter(w);
            w.flush();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            t.process(param, out);
            out.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
